/**
 * 设计图  引用乐府小学生
 */
const {menu,ccclass, property, executeInEditMode} = cc._decorator;
@ccclass
@menu('效果图/DesignView')
@executeInEditMode
export default class DesignView extends cc.Component {

    private _sprite: cc.Sprite = null;
    private _spriteFrame: cc.SpriteFrame = null;

    @property
    private _prefabFileId: string = "";

    @property
    private _opacity: number = 70;

    @property
    private _offsetX: number = 0;

    @property
    private _offsetY: number = 0;

    @property
    private _showTop: boolean = true;

    @property
    private _designUrl: string = "";

    @property({type: cc.SpriteFrame, displayName: "设计图"})
    public set spriteFrame(frame: cc.SpriteFrame) {
        this._updateDesignView(frame);
    }

    public get spriteFrame(): cc.SpriteFrame {
        return this._spriteFrame;
    }

    @property({type: cc.Boolean, displayName: "置顶"})
    public set showTop(top: boolean) {
        this._showTop = top;
        this._updateOrder();
    }

    public get showTop(): boolean {
        return this._showTop;
    }

    @property({type: cc.Integer, displayName: "透明度", slide: true, min : 0, max: 255})
    public set opacity(value: number) {
        this._opacity = value;
        this._updateOpatity();
    }

    public get opacity(): number {
        return this._opacity;
    }

    @property({type: cc.Integer, displayName: "X偏移"})
    public set offsetX(value: number) {
        this._offsetX = value;
        this._updateOffset();
    }

    public get offsetX(): number {
        return this._offsetX;
    }

    @property({type: cc.Integer, displayName: "Y偏移"})
    public set offsetY(value: number) {
        this._offsetY = value;
        this._updateOffset();
    }

    public get offsetY(): number {
        return this._offsetY;
    }

    private _updateDesignView(frame: cc.SpriteFrame) {
        if (this._spriteFrame === frame) {
            return;
        }
        this._designUrl = frame ? frame["_uuid"] : "";
        if (!this._sprite) {
            let viewNode = new cc.Node("DesignView");
            this._sprite = viewNode.addComponent(cc.Sprite);
            viewNode.parent = this.node;
            viewNode["_objFlags"] |= (cc.Object["Flags"].DontSave | cc.Object["Flags"].LockedInEditor | cc.Object["Flags"].HideInHierarchy);

            this._updateOffset();
            this._updateOrder();
            this._updateOpatity();
        }
        this._sprite.spriteFrame = frame;
        this._spriteFrame = frame;
    }

    private _updateOffset() {
        if (!this._sprite) {
            return;
        }
        let node = this._sprite.node;
        node.x = this._offsetX;
        node.y = this._offsetY;
    }

    private _updateOrder() {
        if (!this._sprite) {
            return;
        }
        this._sprite.node.zIndex = this._showTop ? 999 : -1;
    }

    private _updateOpatity() {
        if (!this._sprite) {
            return;
        }
        this._sprite.node.opacity = this._opacity;
    }

    public onLoad() { 
        if (!CC_EDITOR) {
            return;
        }
        if (!this._prefabFileId && this.node["_prefab"]) {
            this._prefabFileId = this.node["_prefab"].fileId;
        }
        if (this._isCurPrefab()) {
            if (this._designUrl) { 
                if (cc.assetManager && cc.assetManager.loadAny) {
                    cc.assetManager.loadAny({type: "uuid", uuid: this._designUrl}, null, (err, spriteFrame: cc.SpriteFrame)=> {
                        if (!err) {
                            this._updateDesignView(spriteFrame);
                        }
                    })
                }
                else {
                    // 兼容2.4.0之前版本
                    cc.loader.load({type: "uuid", uuid: this._designUrl}, null, (err, spriteFrame: cc.SpriteFrame)=> {
                        if (!err) {
                            this._updateDesignView(spriteFrame);
                        }
                    })
                }
            }
            if (this._sprite) {
                this._updateOffset();
                this._updateOrder();
                this._updateOpatity();
            }
        }
        else {
            this.destroy();
        }
    }

    private _isCurPrefab(): boolean {
        let prefab = this.node["_prefab"];
        return prefab && prefab.root && (!this._prefabFileId || this._prefabFileId === prefab.fileId);
    }

    public onEnable() {
        if (this._sprite) {
            this._sprite.enabled = true;
        }
    }

    public onDisable() {
        if (this._sprite) {
            this._sprite.enabled = false;
        }
    }

    public onDestroy() {
        if (this._sprite && !(this._sprite.node["_objFlags"] & cc.Object["Flags"].Destroying)) {
            this._sprite.node.destroy();
        }
    }
}